Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "voltage as a state" option #4507

Merged
merged 8 commits into from
Oct 15, 2024
Merged

Add "voltage as a state" option #4507

merged 8 commits into from
Oct 15, 2024

Conversation

rtimms
Copy link
Contributor

@rtimms rtimms commented Oct 11, 2024

Description

Adds an option "voltage as a state" that can be "false" (default) or "true". If "true" adds an explicit algebraic equation for the voltage. This can be used to reformulate the SPM(e) as a DAE rather than and ODE. This increases the solve time but evaluating the voltage as an explicit state rather than as a function of other state variables can be faster if the time eval is dense.

import pybamm
import numpy as np

t = np.linspace(0, 3600, 100000)
for value in ["true", "false"]:
    model = pybamm.lithium_ion.SPMe({"voltage as a state": value})
    sim = pybamm.Simulation(model, solver=pybamm.IDAKLUSolver())
    sim.solve([0, 3600])
    print("voltage as a state:", value)
    solve_time = sim.solution.solve_time
    print("solve time:", solve_time)
    timer = pybamm.Timer()
    sim.solution["Voltage [V]"](t)
    eval_time = timer.time()
    print("eval time:", eval_time)
    print("total time:", solve_time + eval_time, "\n")

returns

voltage as a state: true
solve time: 3.293 ms
eval time: 8.836 ms
total time: 12.129 ms 

voltage as a state: false
solve time: 2.311 ms
eval time: 687.892 ms
total time: 690.204 ms 

Fixes # (issue)

Type of change

Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #) - note reverse order of PR #s. If necessary, also add to the list of breaking changes.

  • New feature (non-breaking change which adds functionality)
  • Optimization (back-end change that speeds up the code)
  • Bug fix (non-breaking change which fixes an issue)

Key checklist:

  • No style issues: $ pre-commit run (or $ nox -s pre-commit) (see CONTRIBUTING.md for how to set this up to run automatically when committing locally, in just two lines of code)
  • All tests pass: $ python run-tests.py --all (or $ nox -s tests)
  • The documentation builds: $ python run-tests.py --doctest (or $ nox -s doctests)

You can run integration tests, unit tests, and doctests together at once, using $ python run-tests.py --quick (or $ nox -s quick).

Further checks:

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

Copy link

codecov bot commented Oct 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.42%. Comparing base (3bf3ea8) to head (0648a91).
Report is 6 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #4507   +/-   ##
========================================
  Coverage    99.42%   99.42%           
========================================
  Files          299      299           
  Lines        22691    22715   +24     
========================================
+ Hits         22560    22584   +24     
  Misses         131      131           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

valentinsulzer
valentinsulzer previously approved these changes Oct 14, 2024
Copy link
Member

@valentinsulzer valentinsulzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks. Those timings are crazy - is that even with the latest changes from Marc?

@rtimms
Copy link
Contributor Author

rtimms commented Oct 15, 2024

Yep. Evaluating all those asinh terms is slow if you have a large t_eval.

@rtimms rtimms merged commit 47c165d into develop Oct 15, 2024
23 checks passed
@rtimms rtimms deleted the voltage-as-a-state branch October 15, 2024 14:37
pkalbhor pushed a commit to pkalbhor/PyBaMM that referenced this pull request Nov 15, 2024
* add voltage as a state option

* improve coverage

* update CHANGELOG

---------

Co-authored-by: Valentin Sulzer <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants